package com.soft.bams.client;

import com.soft.bams.entity.Account;
import javax.swing.*;


public class RegisterPanel extends JPanel {
    private ATMClient atmClient;

    private JLabel typeLabel = new JLabel("账户类型:");
    private JLabel nameLabel = new JLabel("用户名:");
    private JLabel passwordLabel = new JLabel("密码:");
    private JLabel repasswordLabel = new JLabel("确认密码:");
    private JLabel personIdLabel = new JLabel("身份证号:");
    private JLabel emailLabel = new JLabel("E-Mail:");

    private JComboBox<String> typeCombo = new JComboBox<>(new String[]{"储蓄账户", "信用账户", "可贷款储蓄账户", "可贷款信用账户"});
    private JTextField nameField = new JTextField();
    private JPasswordField passwordField = new JPasswordField();
    private JPasswordField repasswordField = new JPasswordField();
    private JTextField personIdField = new JTextField();
    private JTextField emailField = new JTextField();

    private JButton submitBtn = new JButton("提交");
    private JButton backBtn = new JButton("返回");

    public RegisterPanel(ATMClient atmClient) {
        this.atmClient = atmClient;
        setLayout(null);
        initComponents();
    }

    private void initComponents() {
        // 设置组件位置和大小
        typeLabel.setBounds(50, 30, 100, 25);
        typeCombo.setBounds(160, 30, 200, 25);

        nameLabel.setBounds(50, 70, 100, 25);
        nameField.setBounds(160, 70, 200, 25);

        passwordLabel.setBounds(50, 110, 100, 25);
        passwordField.setBounds(160, 110, 200, 25);

        repasswordLabel.setBounds(50, 150, 100, 25);
        repasswordField.setBounds(160, 150, 200, 25);

        personIdLabel.setBounds(50, 190, 100, 25);
        personIdField.setBounds(160, 190, 200, 25);

        emailLabel.setBounds(50, 230, 100, 25);
        emailField.setBounds(160, 230, 200, 25);

        submitBtn.setBounds(120, 280, 100, 30);
        backBtn.setBounds(240, 280, 100, 30);

        // 添加组件
        add(typeLabel);
        add(typeCombo);
        add(nameLabel);
        add(nameField);
        add(passwordLabel);
        add(passwordField);
        add(repasswordLabel);
        add(repasswordField);
        add(personIdLabel);
        add(personIdField);
        add(emailLabel);
        add(emailField);
        add(submitBtn);
        add(backBtn);

        // 事件处理
        submitBtn.addActionListener(e -> {
            try {
                String password = new String(passwordField.getPassword());
                String repassword = new String(repasswordField.getPassword());
                String name = nameField.getText();
                String personId = personIdField.getText();
                String email = emailField.getText();
                int type = typeCombo.getSelectedIndex();

                String command = String.format("register#%s#%s#%s#%s#%s#%d",
                        password, repassword, name,
                        personId, email,
                        type);

                Object response = atmClient.sendCommand(command);

                if (response instanceof Account) {
                    Account account = (Account) response;
                    atmClient.getBusinessPanel().setAccount(account);
                    atmClient.showPanel("Business");
                    clearFields();
                } else if (response instanceof String) {
                    JOptionPane.showMessageDialog(this, response, "错误", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "注册失败", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });

        backBtn.addActionListener(e -> {
            clearFields();
            atmClient.showPanel("Main");
        });
    }

    private void clearFields() {
        nameField.setText("");
        passwordField.setText("");
        repasswordField.setText("");
        personIdField.setText("");
        emailField.setText("");
        typeCombo.setSelectedIndex(0);
    }
}
